package com.crm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.base.ResponseTest;
import com.crm.domain.dto.ActiveDTO;
import com.crm.domain.entity.Active;
import com.crm.domain.vo.RowsVO;

import com.crm.domain.vo.DataVO;
import com.crm.domain.vo.OneVO;
import com.crm.mapper.ActiveMapper;
import com.crm.service.ActiveService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 15890
 */

@RestController
@RequestMapping("/clues/activity")
@RequiredArgsConstructor
public class ActiveController {
    private final ActiveService activeService;
    private final ActiveMapper activeMapper;

    /**
     * 分页查询活动
     * @param pageNum
     * @param pageSize
     * @param code
     * @param channel
     * @param beginCreateTime
     * @param endCreateTime
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/list")
    public RowsVO list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "code", required = false) String code,
                       @RequestParam(value = "channel", required = false) Integer channel,
                       @RequestParam(value = "params[beginCreatTime]", required = false) String beginCreateTime,
                       @RequestParam(value = "params[endCreatTime]", required = false) String endCreateTime,
                       @RequestParam(value = "params[beginTime]", required = false) String beginTime,
                       @RequestParam(value = "params[endTime]", required = false) String endTime) {
        RowsVO vo = activeService.listByPage(pageNum, pageSize, code, channel, beginCreateTime, endCreateTime, beginTime, endTime);

        return vo;
    }

    /**
     * 新增活动
     * @param dto
     * @return
     */
    @PostMapping
    public ResponseTest add(@RequestBody ActiveDTO dto) {
        activeService.add(dto);
        return new ResponseTest("操作成功", 200);
    }

    /**
     * 删
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public OneVO delete(@PathVariable Integer id) {
        boolean b = activeService.removeById(id);
        return new OneVO("操作成功",200,"");
    }

    /**
     * 改
     * @param dto
     * @return
     */
    @PutMapping
    public OneVO update(@RequestBody ActiveDTO dto){
        Active bean = BeanUtil.toBean(dto, Active.class);
        bean.setUpdateBy("admin");
        bean.setUpdateTime(LocalDateTime.now());
        activeMapper.updateById(bean);
        return new OneVO("操作成功",200,"");
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public OneVO selectById(@PathVariable Long id) {
        Active active = activeMapper.selectById(id);
        OneVO<Active> vo = new OneVO<>();
        vo.setData(active);
        vo.setCode(200);
        vo.setMsg("查询成功");
        return vo;
    }

    /**
     * 根据channel查询
     * @param channel
     * @return
     */
    @GetMapping("/listselect/{channel}")
    public DataVO<Active> selectList(@PathVariable Integer channel) {
        List<Active> actives = activeMapper.selectList(new QueryWrapper<Active>().lambda().eq(Active::getChannel, channel));
        DataVO<Active> activeDataVO = new DataVO<>();
        activeDataVO.setData(actives);
        activeDataVO.setCode(200);
        activeDataVO.setMsg("操作成功");
        return activeDataVO;
    }



}
